package com.jiuzhe.commercial.ui.house;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.amap.api.services.help.Inputtips;
import com.amap.api.services.help.InputtipsQuery;
import com.amap.api.services.help.Tip;
import com.jiuzhe.commercial.R;
import com.jiuzhe.commercial.adapter.SearchResultAdapter;
import com.jiuzhe.commercial.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import leifu.shapelibrary.ShapeView;

/**
 * Created by huanghao on 2018/6/2.
 */

public class SearchResultActivity extends BaseActivity {
    @BindView(R.id.et_input)
    ShapeView etInput;
    @BindView(R.id.iv_x)
    ImageView ivX;
    @BindView(R.id.tv_cancel)
    TextView tvCancel;
    @BindView(R.id.listView)
    ListView listView;
    private SearchResultAdapter searchResultAdapter;
    private String city,area;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.activity_searchresult);
        setTitle("选择地址");
        city = getIntent().getStringExtra("city");
        area = getIntent().getStringExtra("area");
    }

    @OnClick({R.id.tv_cancel,R.id.iv_x})
    public void onclick(View view) {
        switch (view.getId()) {
            case R.id.tv_cancel:
                finish();
                break;
            case R.id.iv_x:
                etInput.setText("");
                break;
        }
    }

    @Override
    protected void loadData() {
        etInput.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (TextUtils.isEmpty(editable.toString())) {
                    ivX.setVisibility(View.GONE);
                } else {
                    ivX.setVisibility(View.VISIBLE);
                    //第二个参数传入null或者“”代表在全国进行检索，否则按照传入的city进行检索
                    String eInput =  etInput.getText().toString();
                    InputtipsQuery inputquery = new InputtipsQuery(area + eInput, city);
                    inputquery.setCityLimit(false);//限制在当前城市
                    Inputtips inputTips = new Inputtips(SearchResultActivity.this, inputquery);
                    inputTips.requestInputtipsAsyn();
                    inputTips.setInputtipsListener(new Inputtips.InputtipsListener() {
                        @Override
                        public void onGetInputtips(List<Tip> list, int i) {
                            searchResultAdapter = new SearchResultAdapter((ArrayList<Tip>) list, SearchResultActivity.this);
                            listView.setAdapter(searchResultAdapter);
                        }
                    });
                }
            }
        });


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Tip tip = (Tip) searchResultAdapter.getItem(i);
                Intent intent = new Intent();
                intent.putExtra("tip", tip);
                setResult(1, intent);
                finish();
            }
        });
    }
}
